//
//  HealthViewController.m
//  ApeExample
//
//  Created by Eton on 2017/9/25.
//  Copyright © 2017年 王-庭协. All rights reserved.
//

#import "HealthViewController.h"
#import "HealthKitManage.h"

#import <CoreMotion/CoreMotion.h>

@interface HealthViewController (){
    UILabel *stepLabel;
    UILabel *distanceLabel;
}

@property (nonatomic, strong) CMPedometer            *pedometer;

@end

@implementation HealthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.z
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(50, 100, 100, 40);
    [btn1 setTitle:@"计步" forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(onClickBtn1) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(50, 160, 100, 40);
    [btn2 setTitle:@"距离" forState:UIControlStateNormal];
    btn2.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(onClickBtn2) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = CGRectMake(50, 340, 100, 40);
    [btn3 setTitle:@"距离" forState:UIControlStateNormal];
    btn3.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:btn3];
    [btn3 addTarget:self action:@selector(onClickBtn3) forControlEvents:UIControlEventTouchUpInside];
    
    stepLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 220, 200, 40)];
    stepLabel.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:stepLabel];
    
    distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 280, 200, 40)];
    distanceLabel.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:distanceLabel];
    
}
- (void)onClickBtn1
{
    HealthKitManage *manage = [HealthKitManage shareInstance];
    [manage authorizeHealthKit:^(BOOL success, NSError *error) {
        
        if (success) {
            NSLog(@"success");
            [manage getStepCount:^(double value, NSError *error) {
                NSLog(@"1count-->%.0f", value);
                NSLog(@"1error-->%@", error.localizedDescription);
                dispatch_async(dispatch_get_main_queue(), ^{
                    stepLabel.text = [NSString stringWithFormat:@"步数：%.0f步", value];
                });
                
            }];
        }
        else {
            NSLog(@"fail");
        }
    }];
}

- (void)onClickBtn2
{
    HealthKitManage *manage = [HealthKitManage shareInstance];
    [manage authorizeHealthKit:^(BOOL success, NSError *error) {
        
        if (success) {
            NSLog(@"success");
            [manage getDistance:^(double value, NSError *error) {
                NSLog(@"2count-->%.2f", value);
                NSLog(@"2error-->%@", error.localizedDescription);
                dispatch_async(dispatch_get_main_queue(), ^{
                    distanceLabel.text = [NSString stringWithFormat:@"公里数：%.2f公里", value];
                });
                
            }];
        }
        else {
            NSLog(@"fail");
        }
    }];
}



//二、使用CoreMotion获取计步信息
- (void)onClickBtn3{
    _pedometer = [[CMPedometer alloc] init];
    if ([CMPedometer isStepCountingAvailable]) {
        //获取昨天的步数与距离数据
        [_pedometer queryPedometerDataFromDate:[NSDate dateWithTimeIntervalSinceNow:-60*60*24*2] toDate:[NSDate dateWithTimeIntervalSinceNow:-60*60*24*1] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
            if (error) {
                NSLog(@"error===%@",error);
            }
            else {
                NSLog(@"步数===%@",pedometerData.numberOfSteps);
                NSLog(@"距离===%@",pedometerData.distance);
            }
        }];
    } else {
        NSLog(@"不可用===");
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
